-------------------------------------------------------------------------------
-- Starts the Xavante Web server.
--
-- See xavante/config.lua and the online documentation for configuration details.
--
-- Authors: Javier Guerra and Andre Carregal
-- Copyright (c) 2004-2007 Kepler Project
--
-- $Id: t_xavante_start,v 1.1 2007/10/31 18:46:03 carregal Exp $
-------------------------------------------------------------------------------
-- Kepler bootstrap file
-- Defines the default directories for Kepler
--
-- $Id: t_kepler_init.lua,v 1.14 2007/10/31 16:50:05 carregal Exp $

-- Library extension used in the system (dll, so etc)
local libext = [[dll]]

-- Kepler applications directory
KEPLER_APPS = [[.\projects\Kepler\1.1\apps]]

-- Kepler configuration directory
KEPLER_CONF = [[.\projects\Kepler\1.1\conf]]

-- Kepler logs directory
KEPLER_LOG = [[.\projects\Kepler\1.1\log]]

-- Kepler C modules directory
KEPLER_LIB = [[.]]

-- Kepler Lua modules directory
KEPLER_LUA = [[.]]

-- Kepler temporary directory
KEPLER_TMP = [[.\projects\Kepler\1.1\tmp]]

-- Kepler default web directory
KEPLER_WEB  = [[.\projects\Kepler\1.1\web]]

--------- end of parameters ------------

-- Allowed globals for CGILua running under Rings
RINGS_CGILUA_GLOBALS = {
    "KEPLER_APPS", "KEPLER_CONF", "KEPLER_LIB", "KEPLER_LOG", "KEPLER_LUA", "KEPLER_TMP", "KEPLER_WEB"
}

package.path = KEPLER_LUA..[[/?.lua;]]..KEPLER_LUA..[[/?/init.lua;]]..package.path
package.cpath = KEPLER_LIB..[[/?.]]..libext..";"..package.cpath


XAVANTE_CFG = KEPLER_CONF.."/xavante/config.lua"
XAVANTE_LOG = KEPLER_LOG.."/xavante.log"
XAVANTE_TMP = KEPLER_TMP
XAVANTE_WEB = KEPLER_WEB

--[[
local log = io.open(XAVANTE_LOG, "a+")
io.stdout = log
io.stderr = log
print = function (...)
  local nargs = select('#', ...)
  for i = 1, nargs-1 do
    log:write((select(i, ...)))
    log:write("\t")
  end
  log:write(select(nargs, ...))
  log:write("\n")
end
--]]

XAVANTE_ISFINISHED = XAVANTE_ISFINISHED or function() return false end

local res, err = pcall(require, "xavante")

if not res then
    io.stderr:write(err .. "\n")
else
    -------------------------------------------------------------------------------
    -- Loads the configuration file and starts Xavante
    --
    -- XAVANTE_ISFINISHED and XAVANTE_TIMEOUT are optional globals that can
    -- control how Xavante will behave when being externally controlled.
    -- XAVANTE_ISFINISHED is a function to be called on every step of Xavante,
    -- XAVANTE_TIMEOUT is the timeout to be used by Copas.
    -------------------------------------------------------------------------------
    io.stdout:write("[Xavante launcher] Starting Xavante...\n")
  
    -- tries to load the user configuration
    res, err = pcall(loadfile, XAVANTE_CFG)
    if res then
        err()
    else
        error("Error loading user config.lua" .. err)
    end  
    res, err = pcall(xavante.start, function (...)
        --log:flush()
        return XAVANTE_ISFINISHED(...)
	end, XAVANTE_TIMEOUT)
    if not res then
        io.stderr:write(err .. "\n")
    else
        io.stderr:write("[Xavante launcher] Xavante stopped\n")
    end
end

--log:close()

